home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / gigo0714.zip / DIGEST.CPP < prev    next >
Text File  |  1995-06-22  |  11KB  |  468 lines

  1. /*
  2.  * DIGEST
  3.  * 
  4.  * Sample program that makes digest message format messages for GIGO mailing
  5.  * lists.  This source code is provided so that others may may improvements
  6.  * on it, or so that you may use it as a base for other projects.
  7.  * 
  8.  * This program will take all *.MLS (mailing list SENT files), and turn them
  9.  * into *.MLD files (mailing list DONE files). A new *.DLQ file will be
  10.  * created; you will want to move it to a new directory specifically for
  11.  * digest list members, and rename it to *.MLQ at the same time. The
  12.  * resulting file will be ready for GIGO to "EXPLODE" as a normal MLQ file.
  13.  * 
  14.  * Jason Fesler  March, 1995    jfesler@wmeonlin.sacbbx.com
  15.  */
  16.  
  17.  
  18. /*
  19.  * For simplicity sake, I include everything, so that I don't have any
  20.  * problems with the compiler knowing what I am talking about.
  21.  */
  22. #include <time.h>
  23. #include <io.h>
  24. #include <fcntl.h>
  25. #include <sys\stat.h>
  26. #include <process.h>
  27. #include <share.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <malloc.h>
  32. #include <conio.h>
  33. #include <ctype.h>
  34. #include <direct.h>
  35. #include <dos.h>
  36. #include <stdarg.h>
  37.  
  38.  
  39. /*
  40.  * Global variables are usually a bad programming practice. However, I don't
  41.  * give a damn.. :-)
  42.  * 
  43.  */
  44.  
  45.  
  46. struct headers {
  47.     char            From[256];
  48.     char            Subject[256];
  49.     char            Date[256];
  50. }               header;
  51. struct configs {
  52.     char            listname[256];
  53.     char            from[256];
  54.     char            receivedline[256];
  55.     char            dateline[256];
  56.     char            fromline[256];
  57.     char            replyto[256];
  58.     char            to[256];
  59. }               config;
  60.  
  61. FILE           *infile;
  62. FILE           *topfile = NULL, *tempfile = NULL, *cfgfile = NULL;
  63.  
  64.  
  65. /*
  66.  * cmpcopy looks to see if string at <source> compares to the string at
  67.  * <token>.  If it does, the parameters after <token> are going to be copied
  68.  * to <dest>.
  69.  */
  70.  
  71.  
  72. void
  73. cmpcopy(char *token, char *source, char *dest)
  74. {
  75.     if (!strncmp(source, token, strlen(token))) {
  76.         source += strlen(token);
  77.         //get past the token itself
  78.             while (*source == ' ')
  79.             source++;
  80.         //get past the spaces
  81.             strcpy(dest, source);
  82.     }
  83. }
  84.  
  85. /*
  86.  * stristr is a case insensitive strstr function.
  87.  * 
  88.  */
  89. char           *
  90. stristr(char *master, char *sub)
  91. {
  92.     int             l;
  93.     l = strlen(sub);
  94.     while (*master) {
  95.         if (!strnicmp(master, sub, l))
  96.             return master;
  97.         master++;
  98.     }
  99.     return NULL;
  100. }
  101.  
  102. char           *
  103. currentunixdate(void)
  104. {
  105.     static char     buf[150];
  106.     struct tm      *time_now;
  107.     time_t          secs_now;
  108.     char            tzone[256];
  109.  
  110.     long            temp;
  111.     strcpy(tzone, "TZ=PST8");
  112.     putenv(tzone);
  113.     tzset();
  114.     time(&secs_now);
  115.     time_now = localtime(&secs_now);
  116.     strftime(buf, sizeof(buf) - 1, "%d %b %y %H:%M:%S ", time_now);
  117.     temp = atol(tzone + 6);
  118.     if (temp > 0)
  119.         strcat(buf, "-");
  120.     else {
  121.         strcat(buf, "+");
  122.         temp /= -1;
  123.     }
  124.     sprintf(buf + strlen(buf), "%02li00", temp);
  125.     return buf;
  126. }
  127.  
  128. char           *
  129. datestring(char *s)
  130. {
  131.     static char     buf[150];
  132.     struct tm      *time_now;
  133.     time_t          secs_now;
  134.     char            tzone[256];
  135.  
  136.     long            temp;
  137.     strcpy(tzone, "TZ=PST8");
  138.     putenv(tzone);
  139.     tzset();
  140.     time(&secs_now);
  141.     time_now = localtime(&secs_now);
  142.     strftime(buf, sizeof(buf) - 1, s, time_now);
  143.     return buf;
  144. }
  145.  
  146. unsigned long filenumber(void)
  147. {
  148.     static char     buf[150];
  149.     struct tm      *time_now;
  150.     time_t          secs_now;
  151.     char            tzone[256];
  152.         static unsigned long returnme;
  153.  
  154.     long            temp;
  155.     strcpy(tzone, "TZ=PST8");
  156.     putenv(tzone);
  157.     tzset();
  158.     time(&secs_now);
  159.     time_now = localtime(&secs_now);
  160.     strftime(buf, sizeof(buf) - 1, "%y%m%d00", time_now);
  161.         returnme=atol(buf);
  162.        return returnme;
  163. }
  164.  
  165.  
  166.  
  167. /*
  168.  * readheaders reads in the top part of the file until it reaches a newline
  169.  * character.
  170.  * 
  171.  */
  172.  
  173.  
  174. void
  175. readheaders(void)
  176. {
  177.     char            line[1024] = " ";
  178.     memset(&header, 0, sizeof(headers));
  179.     while ((*line) && (!feof(infile))) {
  180.  
  181.         /* remember, GIGO stands for garbage in, garbage out. */
  182.         memset(line, 0, sizeof(line));
  183.         fgets(line, sizeof(line) - 1, infile);
  184.  
  185.         /* fgets includes the line terminator; we need to remove it. */
  186.         if (*line)
  187.             if (line[strlen(line) - 1] == '\n')
  188.                 line[strlen(line) - 1] = 0;
  189.         if (*line)
  190.             if (line[strlen(line) - 1] == '\r')
  191.                 line[strlen(line) - 1] = 0;
  192.         if (!*line)
  193.             continue;    /* We got a blank line, or an eof */
  194.         cmpcopy("From:", line, header.From);
  195.         cmpcopy("Subject:", line, header.Subject);
  196.         cmpcopy("Date:", line, header.Date);
  197.     }
  198. }
  199.  
  200. void
  201. readconfig(void)
  202. {
  203.     char            line[1024] = " ";
  204.     int             stage = 0;
  205.  
  206.     cfgfile = fopen("digest.cfg", "rt");
  207.     if (!cfgfile) {
  208.         fprintf(stderr, "DIGEST.CFG not available.\n");
  209.         exit(1);
  210.     }            /* endif */
  211.     memset(&config, 0, sizeof(config));
  212.     while ((*line) && (!feof(cfgfile))) {
  213.         stage++;
  214.         if (stage > 7)
  215.             break;
  216.         /* remember, GIGO stands for garbage in, garbage out. */
  217.         memset(line, 0, sizeof(line));
  218.         fgets(line, sizeof(line) - 1, cfgfile);
  219.  
  220.         /* fgets includes the line terminator; we need to remove it. */
  221.         if (*line)
  222.             if (line[strlen(line) - 1] == '\n')
  223.                 line[strlen(line) - 1] = 0;
  224.         if (*line)
  225.             if (line[strlen(line) - 1] == '\r')
  226.                 line[strlen(line) - 1] = 0;
  227.         if (!*line)
  228.             continue;    /* We got a blank line, or an eof */
  229.         //linename
  230.             // char         from[256];
  231.         //char          receivedline[256];
  232.         //char          dateline[256];
  233.         //char          fromline[256];
  234.         //char          replyto[256];
  235.         switch (stage) {
  236.         case 1:
  237.             sprintf(config.listname, line, currentunixdate());
  238.             printf("List name: %s\n", config.listname);
  239.             break;
  240.         case 2:
  241.             sprintf(config.from, line, currentunixdate());
  242.             printf("\"from\".. %s\n", config.from);
  243.             break;
  244.         case 3:
  245.             sprintf(config.receivedline, line, currentunixdate());
  246.             printf("\"Received:\".. %s\n", config.receivedline);
  247.             break;
  248.         case 4:
  249.             sprintf(config.dateline, line, currentunixdate());
  250.             printf("\"Date:\".. %s\n", config.dateline);
  251.             break;
  252.         case 5:
  253.             sprintf(config.fromline, line, currentunixdate());
  254.             printf("\"From:\".. %s\n", config.fromline);
  255.             break;
  256.         case 6:
  257.             sprintf(config.replyto, line, currentunixdate());
  258.             printf("\"Reply-To:\".. %s\n", config.replyto);
  259.             break;
  260.         case 7:
  261.             sprintf(config.to, line, currentunixdate());
  262.             printf("\"To:\".. %s\n", config.to);
  263.             break;
  264.         }        /* endswitch */
  265.     }
  266. }
  267.  
  268. void
  269. maketopfile(void)
  270. {
  271.     topfile = fopen("digest.top", "wt");
  272.     if (!topfile)
  273.         return;
  274.     fprintf(topfile,
  275.         "%s\n" // dateline
  276.         "%s\n" // fromline
  277.         "%s\n" // replyto
  278.         "Subject: %s Digest\n" // listname
  279.         "%s\n" // to
  280.         "\n"
  281. "%s Digest             %s\n" // Volume yy:Issue nn \ n
  282.         "\n"
  283.         "Today's Topics:\n:"
  284.         "\n", config.dateline, config.fromline, config.replyto, config.listname, config.to,
  285.         config.listname, currentunixdate());
  286. }
  287. void
  288. maketempfile(void)
  289. {
  290.     tempfile = fopen("digest.tmp", "wt");
  291.  
  292. }
  293.  
  294.  
  295.  
  296. /*
  297.  * All of the answer_*() functions are used with the check_multiple(function
  298.  * pointer) command.  check_multiple scans the directory for all .MLS files,
  299.  * and runs the routine specified passing the parameter of the list name in
  300.  * question.
  301.  * 
  302.  */
  303.  
  304. void
  305. answer_mls(char *filename)
  306. {
  307.     char            buf[256];
  308.     int             c;
  309.     sprintf(buf, "%s", filename);
  310.     infile = fopen(buf, "rt");
  311.     if (!infile)
  312.         return;
  313.     readheaders();
  314.     if (!topfile)
  315.         maketopfile();
  316.     if (!topfile) {
  317.         fclose(infile);
  318.         return;
  319.     }
  320.     if (!tempfile)
  321.         maketempfile();
  322.     if (!tempfile) {
  323.         fclose(infile);
  324.         fclose(topfile);
  325.         return;
  326.     }
  327.     fprintf(topfile, "          %s\n", header.Subject);
  328.  
  329.     fprintf(tempfile, "Date: %s\n", header.Date);
  330.     fprintf(tempfile, "From: %s\n", header.From);
  331.     fprintf(tempfile, "Subject: %s\n", header.Subject);
  332.     fprintf(tempfile, "\n");
  333.     while (!feof(infile)) {
  334.         c = fgetc(infile);
  335.         if (c != EOF)
  336.             fputc(c, tempfile);
  337.     }
  338.     fprintf(tempfile, "\n\n------------------------------\n\n");
  339.  
  340.  
  341.     fclose(infile);
  342.     sprintf(buf, filename);
  343.     if (strchr(b